package com.autumn.log.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.type.AnnotationMetadata;

import com.autumn.log.LogEventData;
import com.autumn.log.client.logback.AutumnLoggingClient;
import com.autumn.mq.event.EventTypeEnum;
import com.autumn.mq.event.IEventData;
import com.autumn.mq.event.IEventDataConfigureInfo;
import com.autumn.mq.rabbit.register.AutumnRabbitBeanDefinitionRegisterManager;
import com.autumn.mq.rabbit.register.AutumnRabbitConnectionInfo;
import com.autumn.spring.boot.bean.AbstractImportBeanDefinitionRegister;
import com.autumn.spring.boot.properties.PropertiesConstant;
import com.autumn.util.AutumnContextUtils;
import com.autumn.util.ExceptionUtils;
import com.autumn.util.StringUtils;

/**
 * 
 * @author 老码农
 *
 *         2018-02-01 12:10:25
 */
public class AutumnLoggingClientBeanDefinitionRegister extends AbstractImportBeanDefinitionRegister {

	/**
	 * 
	 * @return
	 *
	 */
	private IEventDataConfigureInfo createEventDataConfigureInfo() {
		return new IEventDataConfigureInfo() {

			@Override
			public String getName() {
				return LogEventData.EVENT_NAME;
			}

			@Override
			public String getRoutingKey() {
				return null;
			}

			@Override
			public String getExchangeSuffix() {
				return null;
			}

			@Override
			public EventTypeEnum getEventType() {
				return EventTypeEnum.DIRECT;
			}

			@Override
			public Class<? extends IEventData> getEventDataType() {
				return LogEventData.class;
			}
		};
	}

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		String rabbitUserName = this.getEnvironment()
				.getProperty(PropertiesConstant.AUTUMN_MESSAGE_RABBIT_MQ_PROPERTIES_PREFIX + ".username");
		if (rabbitUserName == null) {
			this.logger.warn(
					"本地未配置或未能从配置服务器中获取 " + PropertiesConstant.AUTUMN_MESSAGE_RABBIT_MQ_PROPERTIES_PREFIX + " 的配置信息。");
			return;
		}

		String serviceId = this.getEnvironment().getProperty(AutumnContextUtils.PROPERTIES_SPRING_APPLICATION_NAME);
		if (StringUtils.isNullOrBlank(serviceId)) {
			ExceptionUtils.throwConfigureException("未配置 " + AutumnContextUtils.PROPERTIES_SPRING_APPLICATION_NAME);
		}

		AutumnRabbitBeanDefinitionRegisterManager regManager = new AutumnRabbitBeanDefinitionRegisterManager(
				this.getEnvironment(), this.getResourceLoader(), registry);

		

		// 本地不需要配置，根据配置中心自动返回
		AutumnRabbitConnectionInfo info = new AutumnRabbitConnectionInfo();
		info.setBeanPrefix(PropertiesConstant.AUTUMN_MESSAGE_BENA_PREFIX);
		info.setPropertiesPrefix(PropertiesConstant.AUTUMN_MESSAGE_RABBIT_MQ_PROPERTIES_PREFIX);

		Collection<IEventDataConfigureInfo> eventDataInfos = new ArrayList<>();
		eventDataInfos.add(createEventDataConfigureInfo());

		info.setEventDataInfos(eventDataInfos);
		info.setPrimary(false);

		Map<String, Object> map = regManager.getRegisterManager()
				.getEnvironmentPropertiesMap(info.getPropertiesPrefix());

		info.setProperties(map);
		regManager.register(info);

		String beanName = AutumnLoggingClient.LOGGING_BEAN_NAME;
		if (!registry.containsBeanDefinition(beanName)) {
			GenericBeanDefinition definition = regManager.getRegisterManager()
					.createBeanDefinition(AutumnLoggingClient.class, null, null, null);
			definition.setPrimary(true);

			definition.getConstructorArgumentValues()
					.addGenericArgumentValue(new RuntimeBeanReference(info.getMqEventBusBeanName()));
			definition.getConstructorArgumentValues().addGenericArgumentValue(serviceId);
			regManager.getRegisterManager().registerBean(beanName, definition);
		}
	}

}
